package com.xudu.qdadabackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xudu.qdadabackend.common.ErrorCode;
import com.xudu.qdadabackend.common.Result;
import com.xudu.qdadabackend.exception.ServiceException;
import com.xudu.qdadabackend.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.xudu.qdadabackend.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.xudu.qdadabackend.model.entity.QuestionSubmit;
import com.xudu.qdadabackend.model.vo.UserVO;
import com.xudu.qdadabackend.service.QuestionSubmitService;
import com.xudu.qdadabackend.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @className: UserControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-10-10
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private UserService userService;

    @Resource
    private QuestionSubmitService questionSubmitService;



    /**
     * 提交题目接口
     */
    @ApiOperation("提交题目接口")
    @PostMapping("/")
    public Result QuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能点赞
        final UserVO loginUser = userService.getLoginUser();
        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return Result.success(questionSubmitId);
    }


    /**
     * 分页获取题目提交列表（除了管理员外，普通用户只能看到非答案、提交代码等公开信息）
     */
    @ApiOperation("分页获取题目提交列表")
    @PostMapping("/list/page/vo")
    public Result listQuestionSubmitByPageVO(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest) {

        long num = questionSubmitQueryRequest.getPageNum();
        long size = questionSubmitQueryRequest.getPageSize();
        // 从数据库中查询原始的题目提交分页信息
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(num, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        final UserVO loginUser = userService.getLoginUser();
        // 返回脱敏信息
        return Result.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage));
    }




}
